<template>
	<aplid-base-crud ref="crud" class="w-100" v-bind="baseConfig" :key="reloadLog" :api="api" :tableConfig="tableConfig">
		<template v-slot:failureReason="{ row }">
			<span>{{ row.failureReason }}</span>
		</template>
		<template v-slot:errorStatus="{ row }">
			<el-tag type="warning" v-if="row.errorStatus == 0">疑似异常</el-tag>
			<el-tag type="danger" v-if="row.errorStatus == 1">确定异常</el-tag>
			<el-tag type="success" v-if="row.errorStatus == 2">正常</el-tag>
		</template>
	</aplid-base-crud>
</template>

<script setup lang="ts">
import { useConfig } from '@/views/datacenter/hooks/useConfig'
const { reloadLog, getFieldData } = useConfig()

type Props = {
	dataStatus?: string | number
	batchNumber?: string | number
	apiIndex?: string
	appealId?: string
	tableName?: string
	versionNumber?: string
}

const props = withDefaults(defineProps<Props>(), {
	apiIndex: 'view_dpDetails_page',
})
const emit = defineEmits(['onload-data'])
let api = ref({
	index: '',
})
const tableConfig: any = {
	stripe: true,
	border: false,
	tableLayout: 'auto',
	columns: [],
}
let query: any = {
	dataStatusList: [2],
	dataType: props.tableName, // 表名称
	// dataStatus: props.dataStatus,
	batchNumber: props.batchNumber,
	appealId: props.appealId,
}

if (+props.dataStatus == 3) {
	query = {
		batchNumber: props.batchNumber,
		dataStatusList: [3],
	}
}
if (+props.dataStatus == 1) {
	query = {
		batchNumber: props.batchNumber,
		dataStatusList: [1, 4, 5],
	}
}

const baseConfig = {
	pageName: '进度日志',
	dictcodes: [],
	rowKey: 'id',
	auth: {
		index: props.apiIndex,
	},
	paramsObj: query,
	menuBtn: [],
	onLoadFormate: async (arr) => {
		const tableData = arr.map((item) => {
			const dataObject = typeof item.dataObject == 'string' ? JSON.parse(item.dataObject) : item.dataObject
			const data = { ...item, ...dataObject }
			return {
				...data,
			}
		})
		emit('onload-data', tableData)
		return Promise.resolve(Object.freeze(tableData))
	},
}

onMounted(() => {
	getFieldData(props.tableName,props.versionNumber).then((res) => {
		api.value.index = props.apiIndex
		let columnData: any = res || []
		if (props.dataStatus == 3) {
			tableConfig.columns = [...columnData]
		} else {
			tableConfig.columns = [
				...columnData,
				{
					label: '异常状态',
					prop: 'errorStatus',
					width: 120,
					fixed: 'right',
					hide: props.dataStatus != 2,
				},
				{
					label: props.dataStatus == 2 ? '失败原因' : '异常原因',
					prop: 'failureReason',
					width: 320,
					fixed: 'right',
					hide: props.dataStatus == 3,
				},
			]
		}

		if (props.appealId) {
			tableConfig.columns.unshift({
				type: 'selection',
			})
		}
		reloadLog.value = Math.random()
	})
})

watch(
	() => props.dataStatus,
	() => {
		if (+props.dataStatus == 2) {
			query = {
				batchNumber: props.batchNumber,
				dataStatus: props.dataStatus,
			}
		}
		if (+props.dataStatus == 3) {
			query = {
				batchNumber: props.batchNumber,
				dataStatusList: [3, 4, 5],
			}
		}
	},
	{
		immediate: true,
	}
)
</script>

<style scoped></style>
